When a new reference is assigned to str
it replaces the previous reference.
Details of the action:
The word garbage is the correct term for objects that are not refered to by any reference variable. Since they cannot be found by the program, there is no reason for them to exist. This situation is common and not usually a mistake. As a program executes, objects are created. Then when they are no longer needed references to them are discarded. However, a part of the Java system called the garbage collector reclaims the lost objects so that the memory they are made of can be used again.
Can several objects of the same class exist in a program at the same time?